package com.example.front.until;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimesUntil {

    /***
     * 字符串转时间戳
     * @param str
     * @return
     */
    public static long getTimeStamp(String str){
        if (str == null || "".equals(str)){
            return 0l;
        }

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            date = simpleDateFormat.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0l;
        }
    }

    /***
     * 获取当前时间
     * @return
     */
    public static String getDate(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());
    }

    public static String getDate(String format){
        SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
        return df.format(new Date());
    }

    /***
     * 获取当天
     * @return
     */
    public static String nowDay(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());
    }

    /***
     * 获取当月 2022-05
     * @return
     */
    public static String nowYearMonth(){

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");//设置日期格式
        return df.format(new Date());

//        Calendar calendar = Calendar.getInstance();
//        //记得要+1
//        return String.valueOf(calendar.get(Calendar.MONTH) + 1) ;
    }

    public static String getFirstDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayDate = sdf.format(calendar.getTime())+" 00:00:00";
        return firstDayDate;
    }

    public static String getLastDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = 0;
        //2月的平年瑞年天数
        if (month == 2) {
            // 这个api在计算2020年2月的过程中有问题
            lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
        } else {
            lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayDate = sdf.format(calendar.getTime()) + " 23:59:59";
        return lastDayDate;
    }

    //获取季度第一天
    public static String getFirstDayOfQuarter(int month) {

        if (month == 1 || month == 2 || month == 3){
            month = 1;
        }else if (month == 4 || month == 5 || month == 6){
            month = 4;
        }else if(month == 7 || month == 8 || month == 9){
            month = 7;
        }else if(month == 10 || month == 11 || month == 12){
            month = 10;
        }
        return getFirstDayOfMonth(month);
    }

    //获取当前季度
    //获取季度第一天
    public static String getQuarter(int month) {
        String quarter = "1";
        if (month == 1 || month == 2 || month == 3){
            quarter = "1";
        }else if (month == 4 || month == 5 || month == 6){
            quarter = "2";
        }else if(month == 7 || month == 8 || month == 9){
            quarter = "3";
        }else if(month == 10 || month == 11 || month == 12){
            quarter = "4";
        }
        return quarter;
    }

    //获取季度第一天
    public static String getLastDayOfQuarter(int month) {

        if (month == 1 || month == 2 || month == 3){
            month = 3;
        }else if (month == 4 || month == 5 || month == 6){
            month = 6;
        }else if(month == 7 || month == 8 || month == 9){
            month = 9;
        }else if(month == 10 || month == 11 || month == 12){
            month = 12;
        }

        return getLastDayOfMonth(month);
    }



    /***
     * 获取当月 5
     * @return
     */
    public static String nowMonth(){
        Calendar calendar = Calendar.getInstance();
        //记得要+1
        return String.valueOf(calendar.get(Calendar.MONTH) + 1) ;
    }


    /***
     * 获取当年
     * @return
     */
    public static String nowYear(){
        Calendar calendar = Calendar.getInstance();
        return String.valueOf(calendar.get(Calendar.YEAR)) ;
    }

}
